package leetcode.treeCode.week3.leetcode_257;

import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<Integer> path = new ArrayList<>();
        ArrayList<String> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        traverse(root, path, ans);
        return ans ;


    }
    private void traverse(TreeNode node , List<Integer> path , List<String> ans){

        path.add(node.val);
        if (node.left == null && node.right == null) {
            StringBuilder builder = new StringBuilder();
            int size = path.size();
            for (int i = 0; i < size - 1; i++) {
                builder.append(path.get(i));
                builder.append("->");
            }
            builder.append(path.get(size - 1));
            ans.add(builder.toString());
            return;
        }
        if (node.left !=null){
            traverse(node.left, path, ans);
            path.remove(path.size() -1);
        }
        if (node.right !=null){
            traverse(node.right, path, ans);
            path.remove(path.size() -1);
        }
    }
}